﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DevDemo
{
    public partial class UC_GridList : UserControl
    {
        public UC_GridList()
        {
            InitializeComponent();
        }

        public string TypeTitle = null;

        public Dictionary<string, string> listArr = new Dictionary<string, string>();

        private void UC_GridList_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(TypeTitle))
            {
                this.label1.Text = TypeTitle;
            }

            //去除首列
            dataGridView1.RowHeadersVisible = false;

            DataGridViewCellStyle columnHeaderStyle = new DataGridViewCellStyle();

            columnHeaderStyle.BackColor = Color.Beige;

            columnHeaderStyle.Font = new Font("Verdana", 10, FontStyle.Bold);

            dataGridView1.ColumnHeadersDefaultCellStyle = columnHeaderStyle;

            this.dataGridView1.Columns.Add("1", "对象");

            this.dataGridView1.Columns.Add("2", "操作");

            this.dataGridView1.ColumnHeadersVisible = false;

            this.dataGridView1.Columns[1].Width = 60;

            //将模式改为填充
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;


            if (listArr.Count > 0)
            {
                foreach (var item in listArr)
                {
                    DataGridViewRow dr = new DataGridViewRow();

                    foreach (DataGridViewColumn c in this.dataGridView1.Columns)
                    {
                        dr.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);  //给行添加单元格
                    }

                    dr.Cells[0].Value = item.Value;

                    this.dataGridView1.Rows.Add(dr);

                    int index = this.dataGridView1.Rows.Count - 2;

                    Button btn2 = new Button();

                    btn2.Name = "btnRemove" + index.ToString();

                    btn2.Text = "删";

                    btn2.Width = 30;

                    btn2.Click += new EventHandler(btn2_Click);

                    this.dataGridView1.Controls.Add(btn2);

                    this.dataGridView1.Columns[1].Width = btn2.Width + 6;

                    btn2.Location = new System.Drawing.Point(((this.dataGridView1.GetCellDisplayRectangle(1, index, true).Right - 1) - (btn2.Width)), this.dataGridView1.GetCellDisplayRectangle(1, index, true).Y);
                }
            }
        }

        public void btn2_Click(object sender, EventArgs e)
        {
            int RowCount = this.dataGridView1.Rows.Count;

            Button btn = (Button)(sender);

            //这个btn的name是btnRemove打头的

            string suffix = btn.Name.ToString().Substring(9);  //后边那个号，相当于index的string

            this.dataGridView1.Controls.RemoveByKey(btn.Name);

            this.dataGridView1.Controls.RemoveByKey("btnRun" + suffix);

            this.dataGridView1.Controls.RemoveByKey("Containers" + suffix);

            int index = Convert.ToInt32(suffix);

            this.dataGridView1.Rows.RemoveAt(index);

            if (index < RowCount - 2)
            {
                for (int i = index + 1; i < RowCount - 1; i++)
                {
                    Control ctl3 = this.dataGridView1.Controls["btnRemove" + i.ToString()];

                    Button btnRemove = (Button)ctl3;


                    btnRemove.Location = new System.Drawing.Point(btnRemove.Location.X, btnRemove.Location.Y - 23);

                    //改名字的后缀

                    int j = i - 1;

                    btnRemove.Name = "btnRemove" + j.ToString();
                }
            }
        }
    }
}
